package test.core;


import static test.core.SeleneseTestCaseEx.*;
import static test.util.ConstantCollection.*;
import static org.testng.Reporter.*;

import org.testng.Reporter;

import test.core.HandlingError;
import test.util.TestConfig;

public class HandlingError {
	private static final HandlingError INSTANCE = new HandlingError();
	private static TestConfig tc = TestConfig.getInstance();
	private Object[] errorDesc = new Object[3];
	
	
	public static HandlingError getInstance() {
	      return INSTANCE;
	}	
	
	public void setErrorDesc(Exception e) {
		errorDesc[0] = "error";
		errorDesc[1] = e.getMessage();
		errorDesc[2] = null; 
	}
	
	public void setFailDesc(String description) {
		errorDesc[0] = "fail";
		errorDesc[1] = description;
		errorDesc[2] = null;
	}
	
	public void setWarnDesc(String description) {
		errorDesc[0] = "warning";
		errorDesc[1] = description;
		errorDesc[2] = null;
	}
	
	public void checkStepResult() {
		Integer status = STATUS_UNCHECKED;
		
		if (errorDesc[0] == null) {
			status = STATUS_PASS;
			this.addResult(status, "");  //add log
		} else {	
			if (((String) errorDesc[0]).contentEquals("error")) {
				status = STATUS_ERROR;
			} else if (((String) errorDesc[0]).contentEquals("fail")) {
				status = STATUS_FAIL;
			} else if (((String) errorDesc[0]).contentEquals("warning")) {
				status = STATUS_WARNING;
			}
			this.addResult(status, (String) errorDesc[1]); //add log			
		}
		errorDesc[0] = null;
		errorDesc[1] = null;
		errorDesc[2] = null;
	}
	
	/**
	 * 
	 * @param status
	 * @param description
	 */
	public void addResult(Integer status, String description){
		switch(status) {
		case STATUS_ERROR: 
			System.err.println("STATUS: " + getStatusName(status));
			System.err.println(description);
			Reporter.log(description);
			break;
		default:
			if (tc.getEnv("addpasssteptoresult").toLowerCase().contentEquals("true")) {
	    		System.out.println("STATUS: " + getStatusName(status));
	    		org.testng.Reporter.log("STATUS: " + getStatusName(status));
	    	}
			break;
		}
	}

}
